<?php

namespace Addons\OaSystem\Controller;

use Think\ApiBaseController;

class ApiLogisticController extends ApiBaseController
{
    var $openid;
    var $appid;
    var $cur_emp_id;
    var $initial_check_pass;

    public function __construct()
    {
        $this->initial_check_pass = false;
        parent::__construct();
        $this->openid = get_openid();
        $this->appid = get_app_info()['appid'];
        if ($this->openid == -1) {
            $return['errcode'] = '40003';
            echo api_return($return['errcode'], null, error_msg($return));
            exit;
        }
        $map['userid'] = $this->mid;
        $userEmp = M('OaUserEmployee')->where($map)->find();
        if (empty($userEmp)) {
            $return['errcode'] = '50001';
            echo api_return($return['errcode'], null, error_msg($return, "找不到对应的员工号"));
            exit;
        }
        $this->cur_emp_id = $userEmp['emp_id'];
        $this->initial_check_pass = true;
    }

    public function GetFoodmenuInWeek () {
        if(!$this->initial_check_pass) {
            return;
        }
        $data = array();
        for($i = 1; $i < 8; $i++){
            $map['dayweek'] = $i;
            $tmp = D('OaDayFoodmenuLogisticView')->where($map)->select();
            array_push($data,$tmp);
        }
        echo api_return(0,$data);
    }
    private function generateOderSn ($oderId,$type) {
        if (empty($type) || empty($oderId)){
            return '';
        }
        $typeStr = array('','ST','XMB','PW');
        $date = date('ymd', time());
        $tmpId = ($oderId > 1000000)?str($oderId):substr(strval((intval(1000000)+intval($oderId))),1,6);
        $sn = $typeStr[$type].$date.$tmpId;
        return $sn;
    }
    public function CreateNewOrder () {
        if (empty(I('data'))) {
            $return['errcode'] = '61451';
            echo api_return($return['errcode'],$res,error_msg($return));
            return;
        }

        $items = json_decode(I('data'),true);
        $time = NOW_TIME;
        $ids = array();
        foreach ($items as $item){
            $data = $item;
            $data['appid'] = $this->appid;
            $data['time'] = $time;
            $data['is_del'] = 0;
            $data['status'] = 0;
            $ret = M('OaServiceOrder')->add($data);
            if($ret === false) {
                $return['errcode'] = '61451';
                echo api_return($return['errcode'], $res, error_msg($return, 'add failed!!'));
            }else {
                $ids[] = $ret;
                $data = M('OaServiceOrder')->where(array('id'=>$ret))->find();
                $data['order_no'] = $this->generateOderSn($data['id'], $data['type']);
                M('OaServiceOrder')->save($data);
                $content = D('OaOrderServiceView')->where(array('id'=>$ret))->find();
                D('OaEmpMsg')->sendServiceOrderMsg($content['emp_id'],$content,true);
            }
        }
        echo api_return(0,$ids);
    }

    public function GetGoodsList () {
        if (empty(I('type'))){
            $return['errcode'] = '61451';
            echo api_return($return['errcode'],$res,error_msg($return));
            return;
        }
        $map['type'] = I('type');
        $data = M('OaLogisticsService')->where($map)->select();

        echo api_return(0,$data);
    }

    public function GetTicketWaiterData () {
        $app_url = substr($_SERVER["REQUEST_URI"],18);
        $page = I('page');
        $row = I('row');
        $status = json_decode(I('status'),true);
        $node = M('OaNode')->where(array('app_url'=>$app_url))->find();
        $data = array();
        if (empty($node)) {
            $data['isWaiter'] = false;
            var_dump($_SERVER["REQUEST_URI"]);
            var_dump($app_url);
        } else {
            if(checkNodeAccessRight($node['id'],$this->cur_emp_id) === false) {
                $data['isWaiter'] = false;
                $map['type'] = 3;
                $map['emp_id'] = $this->cur_emp_id;
                $map['$status'] = array('in',$status);
                $data['count'] = M('OaServiceOrder')->where($map)->count();
                $data['order'] = M('OaServiceOrder')->where($map)->order('time desc')->page($page,$row)->select();
            } else {
                $data['isWaiter'] = true;
                $map['type'] = 3;
                $map['$status'] = array('in',$status);
                $data['count'] = M('OaServiceOrder')->where($map)->count();
                $data['order'] = M('OaServiceOrder')->where($map)->order('time desc')->page($page,$row)->select();
            }
        }

        echo api_return(0,$data);
    }

    public function GetServiceOrderList () {
        if (empty(I('type'))){
            $return['errcode'] = '61451';
            echo api_return($return['errcode'],$res,error_msg($return));
            return;
        }

        if (!empty(I('timerange'))) {
            $timeRange = json_decode(I('timerange'),true);
            $map['date'] = array('between',array($timeRange['s'],$timeRange['e']));
        }
        $map['type'] = I('type');
        $map['emp_id'] = $this->cur_emp_id;

        if(!empty(I('page'))) {
            $page = I('page');
            $row = I('row');
            $data = M('OaServiceOrder')->where($map)->order('time desc')->page($page,$row)->select();
        } else {
            $data = M('OaServiceOrder')->where($map)->order('time desc')->select();
        }

        echo api_return(0,$data);
    }

    public function UpdateOrder() {
        if (empty(I('data'))){
            $return['errcode'] = '61451';
            echo api_return($return['errcode'],$res,error_msg($return));
            return;
        }

        $data = json_decode(I('data'),true);
        if(empty($data['id'] )){
            $return['errcode'] = '61451';
            echo api_return($return['errcode'],$res,error_msg($return,'id is null!!'));
            return;
        }
        $res = M('OaServiceOrder')->save($data);
        if($res === false) {
            $return['errcode'] = '61451';
            echo api_return($return['errcode'],$res,error_msg($return,'save failed!!'));
        }else {
            echo api_return(0,null);
        }

    }
}
